今天要繼續來看註冊功能的後端處理了,
首先要接受前端頁面傳過來的欄位值.
#使用POST接值(以密碼欄位當範例)
$password = $_POST["user_pw"];
接完值之後,針對密碼我先進行正規化過濾以及加密,
若不符合格式便跳回註冊頁.
if(!check_text($password)){
echo "<script>alert('密碼格式錯誤');</script>";
echo "<script>window.location.href='$url';</script>";
return null;
}else{
$password = md5($password);//加密
}
#正規化的方法
function check_text($temp_text){
if(preg_match('/^[A-Za-z0-9_]{1,20}+$/', $temp_text)==0){
return false;
}else{
return true;
}
}
欄位處理完後,再來就是要確認這個帳號是否註冊過了,若有便跳回註冊頁
//PDO連線
$conn = new PDO ( "mysql:host=$servername;dbname=$dbname" , $username , $password_server );
$conn->exec("set names utf8");
//設置PDO錯誤模式為異常
$conn->setAttribute( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION );
//搜尋是否有重覆帳號
$stmt2 = $conn->prepare( "SELECT COUNT(*) AS ACCOUNT_COUNT FROM USER_DATA WHERE USER_ACCOUNT = :USER_ACCOUNT" );
$stmt2->bindParam ( ':USER_ACCOUNT' , $user_account );
$stmt2 -> execute();
$row=$stmt2->fetchAll(PDO::FETCH_ASSOC);
//是否搜尋到
if($row[0]['ACCOUNT_COUNT'] == 1){
echo "<script>alert('帳號重覆');</script>";
echo "<script>window.location.href='$url';</script>";
return null;
}
通過以上重重考驗,最後是註冊的橋段.
// 預處理SQL 並綁定參數-新增帳號
$stmt = $conn->prepare( "INSERT INTO USER_DATA (USER_NAME, USER_ACCOUNT, PASSWORD, DESCRIPTION, CREATE_DATE, UPDATE_DATE)
VALUES (:USER_NAME, :USER_ACCOUNT, :PASSWORD, :DESCRIPTION, NOW(), NOW())" );
$stmt->bindParam ( ':USER_NAME' , $user_name );
$stmt->bindParam ( ':USER_ACCOUNT' , $user_account );
$stmt->bindParam ( ':PASSWORD' , $password );
$stmt->bindParam ( ':DESCRIPTION' , $description );
$stmt -> execute();
註冊完成後就可以回到登入頁面了~
這是我註冊功能的範例~下一章會講登入~
下次見~